// pages/classify/classify.js
const
    app = getApp(),
    globalData = app.globalData,
    api = require('../../api/index');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isIpx: globalData.isIpx,
        visible: false,
        list: [],
        status: 0,
        filter: {
            keywords: "",
            brandId: "",
            categoryId: "",
            types: [1, 2, 3],
            goodsOnState: 1,
            goodsSortByEnum: 1,
            currenetPageIndex: 1,
            pageSize: 10,
            recordCount: 0
        },
        active: 0,
        typeList: [{ Title: '全部', Id: '' }],
        imageBaseUri: globalData.imageBaseUri,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(this.data.isIpx, 'isIpx')
        this.reload()
    },

    reload() {
        this.getGoodsList();
        this.getTypes();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.hasMore) {
            this.setData({
                'filter.currenetPageIndex': ++this.data.filter.currenetPageIndex
            });

            this.getGoodsList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    showMask() {
        this.setData({
            visible: true
        })
    },
    hideMask() {
        this.setData({
            visible: false
        })
    },
    toSearch() {
        wx.navigateTo({
            url: '/pages/search/search',
        })
    },

    // 获取商品列表
    async getGoodsList() {
        let obj = this.data.filter;
        wx.showLoading({
            title: '内容加载中',
        })
        try {
            let res = await api.goods.getGoodsPages(obj),
                data = res.data.ResData || {};
            console.log(res, '商品列表')
            if (this.data.filter.currenetPageIndex > 1) {
                data.Items = this.data.list.concat(data.Items);
            }

            if (data.Items&&data.Items.length) {
                data.Items.map(item => {
                    if (item.MarketingTypes.toString().indexOf('2') != -1) {
                        item.isSeckill = true;
                    } else {
                        item.isSeckill = false;
                    }
                    if (item.MarketingTypes.toString().indexOf('3') != -1) {
                        item.isGroup = true;
                    } else {
                        item.isGroup = false;
                    }
                })
            }


            this.setData({
                list: data.Items ? data.Items : [],
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
                status: 2
            })

        } catch (e) {
            console.log(e, '获取商品类别失败')
            this.setData({
                status: 1
            })
        } finally {
            wx.hideLoading()
        }
    },

    // 获取商品分类
    async getTypes() {
        try {
            let res = await api.goods.goodsType(),
                data = res.data.ResData || [];
            // arr = [{ Title: '全部', Id: '' }];
            console.log(res, '商品分类列表')
            // let obj = { Title: '全部', Id: '' };
            // data = arr.push(data);
            this.setData({
                typeList: this.data.typeList.concat(data),
                status: 2
            })
        } catch (e) {
            console.log(e, '获取商品分类列表失败')
        }
    },

    // 切换分类
    async changeType(e) {
        let id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index;

        this.setData({
            active: index,
            filter: {
                keywords: "",
                brandId: "",
                categoryId: id,
                types: [1, 2, 3],
                goodsOnState: 1,
                goodsSortByEnum: 1,
                currenetPageIndex: 1,
                pageSize: 10,
                recordCount: 0
            },
            list: []
        })
        this.hideMask();
        this.getGoodsList();
        wx.pageScrollTo({ scrollTop: 0 });
    }
})